<?php
/**
 * @version : 1.0
 * @package : lib/struct
 * @name :  CDatabaseSession
 * @todo:  Lớp  mở rộng của CSession. Lưu session trong database
*/
class CDatabaseSession extends CSession
{
	public $table;
	public $values = array();
	function __construct()
	{
		parent::__construct();
		$this->table = CTable('session');
		
		$values = $this->table->select_all(array('session_id'=>$this->id));
		$this->values = array();
		foreach($values as $value)
		{
			eval('$this->values[$value[\'key\']] = '.$value['value'].';');
		}
		srand(microtime(true)*1000);
		if(rand(0,10000)%100 == 0)
		{
			$this->table->delete_all(ELT('last_active_time', EInt(time()-3600*12)));
		}
	}
	function get($name)
	{
		if(isset($this->values[$name]))
		{
			return $this->values[$name];
		}
	}
	function set($name, $value)
	{
		if(isset($this->values[$name]))
		{
			$this->table->update(array('value'=>var_export($value,1), 'last_active_time'=>time()), array('session_id'=>$this->id, 'key'=>$name));
		}
		else
		{
			$this->table->insert(array('value'=>var_export($value, 1), 'session_id'=>$this->id, 'key'=>$name, 'last_active_time'=>time(), 'time'=>time()));
		}
		$this->values[$name] = $value;
	}
	function delete($name)
	{
		unset($this->record[$name]);
		$this->table->delete_all(array('session_id'=>$this->id, 'key'=>$name));
	}
	function close()
	{

	}
}
?>